import Vue from 'vue'
import Vuex from 'vuex'
import api from '@/api'
import addresses from '@/api/addresses'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		store: {},//商户
		cart: [],//购物车
		orderType: 'takein',//取餐形式 自取、外卖
		address: {},//当前选择地址
		addresses: addresses,//地址栏
		member: {},//用户信息
		order: {}//订单
	},
	getters: {
		isLogin: state => Object.keys(state.member).length > 0	//是否登录
	},
	mutations: {
		SET_ORDER_TYPE(state, type) {//设置取餐形式
			state.orderType = type
		},
		SET_MEMBER(state, member) {//赋予member用户信息值
			state.member = member
		},
		SET_ADDRESS(state, address) {//设置地址
			state.address = address
		},
		SET_ADDRESSES(state, addresses) {//设置地址列表
			state.addresses = addresses
		},
		SET_STORE(state, store) {//选择商户
			state.store = store
		},
		SET_CART(state, cart) {//设置购物车
			state.cart = cart
		},
		REMOVE_CART(state) {//清空购物车商品
			state.cart = []
		},
		SET_ORDER(state, order) {//设置订单
			state.order = order
		}
	},
	actions: {
		async getStore({commit}) {
			const store = await api('store')//引用了api中的数据
			commit('SET_STORE', store)
		}
	}
})
//抛出store
export default store